"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 文件目录
Case Name   : 备机实例数据目录属组正确权限修改恢复
Create At   : 2020/07/23
Owner       : @li-xin12345
Description :
    1. 修改备机数据库实例目录权限
    2. 重启数据库
    3. 对备机进行重建
    4. 恢复备机数据库实例目录权限
    5. 重启数据库
Expect      :
    1. 修改备机数据库实例目录权限成功
    2. 重启数据库备机失败
    3. 备机重建失败
    4. 恢复备机数据库实例目录权限成功
    5. 重启数据库成功
History     :
    modified by @wan005 at 2022/01/21:优化用例去除-R 优化注释信息
"""

import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class IndexFileDamaged(unittest.TestCase):
    logger.info('----------------------------Reliability_Dbsys_Case183开始执行-----------------------------')
    standbyRoot = Node(node='Standby1Root')
    standbyNode = Node(node='Standby1DbUser')
    Constant = Constant()
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def test_Index_file_damaged(self):
        # 修改备机数据库实例目录权限
        logger.info('----------------------------修改备机数据库实例目录权限-----------------------------')
        excute_cmd = f'chmod 000 {macro.DB_INSTANCE_PATH}'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, msg)

        # 重建备机
        logger.info('----------------------------重建备机-----------------------------')
        msg = self.sh_stadb.executeGsctl('build', self.Constant.PERMISSION_DENY_MSG, '')
        logger.info(msg)
        self.assertTrue(msg)

        # 恢复备机数据库实例目录权限
        logger.info('----------------------------恢复备机数据库实例目录权限-----------------------------')
        excute_cmd = f'chmod 700 {macro.DB_INSTANCE_PATH}'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, msg)

        logger.info('----------------------------重建备机-----------------------------')
        msg = self.sh_stadb.executeGsctl('build', self.Constant.BUILD_SUCCESS_MSG, '')
        logger.info(msg)
        self.assertTrue(msg)

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        logger.info('----------------------------恢复备机数据库实例目录权限-----------------------------')
        excute_cmd = f'chmod 700 {macro.DB_INSTANCE_PATH}'
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)

        logger.info('----------------------------重建备机-----------------------------')
        msg = self.sh_stadb.executeGsctl('build', self.Constant.BUILD_SUCCESS_MSG, '')
        logger.info(msg)

        logger.info('----------------------------Reliability_Dbsys_Case183执行完成-----------------------------')
